import { action, computed, observable } from "mobx";
import { Toast, Tools } from 'weaver-mobile';
import app from '../../apis/app.js';
import ListViewStore from "../../common/list-view/Store";
import setTitle from '../../utils/setTitle';
import { TABKEYS } from '../../constants/form';

const getLabel = Tools.getLabel;

export default class FormStore {
  @observable loading = false;
  @observable leftDatas = [];
  @observable pageCount = 1;
  @observable sortParams = null;
  @observable creatSearchValue = '';
  @observable creatGroupid = '';
  @observable shareGroupid = '';
  @observable shareSearchValue = '';
  @observable creatAppLvpStore = new ListViewStore();
  @observable shareAppLvpStore = new ListViewStore();

  @computed get groupTypes() {
    return this.leftDatas.filter(d => {
      const isGroupName = d.groupName.toLowerCase().indexOf((this.searchValue || '').toLowerCase()) > -1;
      const isCreator = d.creator.toLowerCase().indexOf((this.searchValue || '').toLowerCase()) > -1;

      return isGroupName || isCreator;
    });
  }

  @action init = () => {
    this.leftDatas = [];
    this.creatSearchValue = '';
    this.shareSearchValue = '';

    setTitle(getLabel(523179, '数据收集'));

    this.creatAppLvpStore.clearData();
    this.shareAppLvpStore.clearData();

    Promise.all([
      app.getLeftDatas(),
    ]).then(([res]) => {
      if (res.data.length > 0) {

        this.leftDatas = res.data.filter(data => data.freeCount && data.freeCount !== '0');
        this.getApps(TABKEYS.MYCREATE);
        this.getApps(TABKEYS.SHAREME);
      }
    });
  }

  changeSearchValue = (key, text) => {
    if (key === TABKEYS.MYCREATE) {
      this.creatSearchValue = text;
    } else {
      this.shareSearchValue = text;
    }

    this.getApps(key);
  }

  changeGroupId = (key, groupid) => {
    if (key === TABKEYS.MYCREATE) {
      this.creatGroupid = groupid;
    } else {
      this.shareGroupid = groupid;
    }


    this.getApps(key);
  }

  getApps = (key) => {
    let searchText = '';
    let groupid = '';
    let rightType = '';

    if (key === TABKEYS.MYCREATE) {
      searchText = this.creatSearchValue;
      groupid = this.creatGroupid;
      rightType = '0';
    } else {
      searchText = this.shareSearchValue;
      groupid = this.shareGroupid;
      rightType = '1';
    }

    const _getDataKey = () => app.getApps({
      selectedType: 'FREE',
      searchText,
      groupid,
      rightType 
    }).then(result => {
      return result.sessionkey.datas;
    });

    if (key === TABKEYS.MYCREATE) {
      return this.creatAppLvpStore.getData(_getDataKey);
    }

    return this.shareAppLvpStore.getData(_getDataKey);
  }

  @action onFillinEnable = (_key, appid, fillinEnable) => {
    app.fillinEnable({
      appid,
      enable: !fillinEnable
    }).then(() => {
      this.fillinEnable = !fillinEnable;
      if (_key === TABKEYS.MYCREATE) {
        this.creatAppLvpStore.updateDataSource(datas => this.updateDataMap(datas, appid, fillinEnable));
      } else {
        this.shareAppLvpStore.updateDataSource(datas => this.updateDataMap(datas, appid, fillinEnable));
      }
    });
  }

  updateDataMap = (datas, appid, fillinEnable) => {
    const app = datas.find(({ id }) => appid === id);

    if (app) {
      app.processspan = fillinEnable ? '0' : '1';
    }

    Toast.success(getLabel(523251, "操作成功"), 1);
    return datas;
  }
}